package com.dede.customdrawable

import android.content.res.ColorStateList
import android.graphics.Color
import android.graphics.Rect
import android.graphics.drawable.ColorDrawable
import android.graphics.drawable.RippleDrawable
import android.os.Build
import android.os.Bundle
import android.support.v4.view.PagerAdapter
import android.support.v4.view.ViewPager
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

    private val colors by lazy {
        arrayListOf(0xFF109D59, /*0xFF109D59*/0xFFFF4081, 0xFFFF1643, 0xFFFF5622, 0xFF039BE5, 0xFF9D27B0)
    }

    private val titles by lazy {
        arrayListOf("首页", "游戏", "影视", "音乐", "图书", "报刊")
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            // 全透明状态栏
            window.decorView.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE
            window.statusBarColor = Color.TRANSPARENT
        }

        setSupportActionBar(tool_bar)

        val drawable = CircularRevealDrawable(0xFF109D59.toInt())
        app_bar.background = drawable

        val viewPagerAdapter = ViewPagerAdapter(titles)
        view_pager.adapter = viewPagerAdapter
        tab_layout.setupWithViewPager(view_pager)
        val tabGroup = tab_layout.getChildAt(0) as ViewGroup
        view_pager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {
            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {
            }

            override fun onPageSelected(position: Int) {
                val tab = tabGroup.getChildAt(position)
                val color = colors[position].toInt()
                val rect = Rect()
                tab.getGlobalVisibleRect(rect)
                drawable.play(color, (rect.left + rect.width() / 2).toFloat(),
                        (rect.top + rect.height() / 2).toFloat())
            }
        })
    }

    class ViewPagerAdapter(private val titles: ArrayList<String>) : PagerAdapter() {
        override fun isViewFromObject(view: View, `object`: Any) = view == `object`

        override fun getCount() = titles.size

        override fun instantiateItem(container: ViewGroup, position: Int): Any {
            val imageView = ImageView(container.context)
            val rippleDrawable = RippleDrawable(
                    ColorStateList.valueOf(Color.GRAY),
                    ColorDrawable(Color.WHITE),
                    null)
            imageView.background = rippleDrawable
            imageView.isClickable = true
            container.addView(imageView, ViewPager.LayoutParams())
            return imageView
        }

        override fun getPageTitle(position: Int) = titles[position]

        override fun destroyItem(container: ViewGroup, position: Int, `object`: Any) {
            container.removeView(`object` as View?)
        }
    }
}
